// pages/department/index.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
let ddmSort;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        doctors: [],
        departments: [],
        page: {
            pageSize: 10,
            pageNo: 1,
            query: "",
            dept: 0
        },

        options1: [{
            text: '默认科室',
            value: -1,
            checked: true
        }],
        sort: '默认科室',
        sortShow: false,
    },

    filterTap(e) {
        ddmSort && ddmSort.show()
        this.setData({
            sortShow: true
        })
    },

    sortItemClick(e) {
        console.log(e.detail)
        this.setData({
            sort: e.detail.text
        })
        this.sortClose()
    },
    sortClose() {
        this.setData({
            sortShow: false
        })
    },
    to_consult(doctorId) {
        wx.navigateTo({
        url: '../to_consult/consult/index?doctorId='+doctorId,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDoctors()
        this.getDepartments()

        ddmSort = this.selectComponent("#ddmSort");
    },
    getDepartments() {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "GET",
            showLoading: true,
            url: `/api/department/list`,
            message: "正在获取...",
            // data:_this.data.card,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                let options = _this.data.options1
                for (let i = 0; i < res.data.data.length; i++) {
                    for (let j = 0; j < res.data.data[i].children.length; j++) {
                        options.push({
                            text: res.data.data[i].children[j].dept_name,
                            value: res.data.data[i].children[j].id
                        })
                    }
                }
                _this.setData({
                    departments: res.data.data,
                    options1: options
                })
                console.log(_this.data.options1)
            }
        }).catch(err => {
            console.log(err)
        });
    },
    getDoctors() {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "POST",
            showLoading: true,
            url: `/api/doctor/list`,
            message: "正在获取...",
            data: _this.data.page,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                _this.setData({
                    doctors: res.data.data.pageData
                })
                console.log(res.data.data.pageData)
            }
        }).catch(err => {
            console.log(err)
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})